package com.ktgj.other.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ktgj.common.core.utils.Pinyin4jUtil;
import com.ktgj.other.domain.ChinaCity;
import com.ktgj.other.domain.RcAirport;
import com.ktgj.other.domain.RcTrain;
import com.ktgj.other.mapper.RcAirportMapper;
import com.ktgj.other.mapper.RcTrainMapper;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.other.mapper.RcCityMapper;
import com.ktgj.other.domain.RcCity;
import com.ktgj.other.service.IRcCityService;

/**
 * 专车城市Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-27
 */
@Service
public class RcCityServiceImpl implements IRcCityService 
{
    @Autowired
    private RcCityMapper rcCityMapper;

    @Autowired
    private RcAirportMapper rcAirportMapper;

    @Autowired
    private RcTrainMapper rcTrainMapper;

    /**
     * 查询专车城市
     * 
     * @param cityid 专车城市ID
     * @return 专车城市
     */
    @Override
    public RcCity selectRcCityById(Long cityid)
    {
        return rcCityMapper.selectRcCityById(cityid);
    }

    /**
     * 查询专车城市列表
     * 
     * @param rcCity 专车城市
     * @return 专车城市
     */
    @Override
    public List<RcCity> selectRcCityList(RcCity rcCity)
    {
        return rcCityMapper.selectRcCityList(rcCity);
    }

    /**
     * 新增专车城市
     * 
     * @param rcCity 专车城市
     * @return 结果
     */
    @Override
    public int insertRcCity(RcCity rcCity)
    {
        return rcCityMapper.insertRcCity(rcCity);
    }

    /**
     * 修改专车城市
     * 
     * @param rcCity 专车城市
     * @return 结果
     */
    @Override
    public int updateRcCity(RcCity rcCity)
    {
        return rcCityMapper.updateRcCity(rcCity);
    }

    /**
     * 批量删除专车城市
     * 
     * @param cityids 需要删除的专车城市ID
     * @return 结果
     */
    @Override
    public int deleteRcCityByIds(Long[] cityids)
    {
        return rcCityMapper.deleteRcCityByIds(cityids);
    }

    /**
     * 删除专车城市信息
     * 
     * @param cityid 专车城市ID
     * @return 结果
     */
    @Override
    public int deleteRcCityById(Long cityid)
    {
        return rcCityMapper.deleteRcCityById(cityid);
    }

    //城市服务列表
    @Override
    public Map<String, List<Map<String, Object>>> cityService() {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<RcCity> rcCities = this.selectRcCityList(null);
        List<RcAirport> rcAirports = rcAirportMapper.selectRcAirportList(null);
        List<RcTrain> rcTrains = rcTrainMapper.selectRcTrainList(null);
        rcCities.forEach(item -> {
            Map<String, Object> map = new HashMap<>();
            map.put("cityid", item.getCityid());
            map.put("cityname", item.getCityname());
            map.put("geolocation", item.getGeolocation());
            map.put("ishotcity", item.getIshotcity());
            map.put("citypinyin", item.getCitypinyin());
            List<RcAirport> airports = rcAirports.stream().filter(airport -> airport.getCityid().equals(item.getCityid())).collect(Collectors.toList());
            List<RcTrain> trains = rcTrains.stream().filter(train -> train.getCityid().equals(item.getCityid())).collect(Collectors.toList());
            map.put("airport", airports);
            map.put("train", trains);
            mapList.add(map);
        });
        mapList.forEach(item -> {
            String initials = item.get("citypinyin").toString().substring(0, 1).toUpperCase();
            item.put("initials", initials);
        });
        mapList.sort(Comparator.comparing(item -> item.get("citypinyin").toString()));
        Map<String, List<Map<String, Object>>> map = mapList.stream().collect(Collectors.groupingBy(item -> item.get("initials").toString(), LinkedHashMap::new, Collectors.toCollection(ArrayList::new)));
        return map;
    }

    //查询中国城市（市、县、州）
    @Override
    public List<ChinaCity> queryCityList() {
        List<ChinaCity> chinaCityList = new ArrayList<>();
        List<ChinaCity> chinaCities = rcCityMapper.queryCityList();
        chinaCities.forEach(item -> {
            if (item.getPid() == 0) {
                String substring = item.getName().substring(item.getName().length() - 1);
                switch (item.getName()) {
                    case "北京市":
                        if (substring.equals("市")) {
                            item.setName(item.getName().replace("市", ""));
                        }
                        chinaCityList.add(item);
                        break;
                    case "天津市":
                        if (substring.equals("市")) {
                            item.setName(item.getName().replace("市", ""));
                        }
                        chinaCityList.add(item);
                        break;
                    case "上海市":
                        if (substring.equals("市")) {
                            item.setName(item.getName().replace("市", ""));
                        }
                        chinaCityList.add(item);
                        break;
                    case "重庆市":
                        if (substring.equals("市")) {
                            item.setName(item.getName().replace("市", ""));
                        }
                        chinaCityList.add(item);
                        break;
                }
            } else {
                if (!item.getName().equals("市辖区") && !item.getName().equals("城区") && !item.getName().equals("省直辖行政单位")) {
                    if (item.getPid() != 110000 && item.getPid() != 120000 && item.getPid() != 310000 && item.getPid() != 500000 && item.getPid() != 990000) {
                        String pid = item.getPid().toString();
                        String substring = pid.substring(pid.length() - 2);
                        if (substring.equals("00")) {
                            String sub = item.getName().substring(item.getName().length() - 1);
                            if (sub.equals("市")) {
                                item.setName(item.getName().replace("市", ""));
                            }
                            chinaCityList.add(item);
                        }
                    }
                }
            }
        });
        return chinaCityList;
    }

    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public static String toPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }
}
